package com.example.mytestapplication

import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.content.Intent

class BluetoothController {
    private val mAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()

    /**
     * 是否支持蓝牙
     * @return true 支持, false 不支持
     */
    fun isSupportBluetooth() = mAdapter != null

    /**
     * 获取蓝牙状态
     * @return true 开启, false 关闭
     */
    fun getBluetoothStatus() = mAdapter?.isEnabled ?: false

    /**
     * 开蓝牙
     */
    fun turnOnBluetooth(activity: Activity, requestCode: Int) {
        val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
        activity.startActivityForResult(intent, requestCode)
        mAdapter?.enable()
    }

    /**
     * 关蓝牙
     */
    fun turnOffBluetooth(activity: Activity, requestCode: Int) {
        mAdapter?.disable()
    }

    fun findDevice() {
        mAdapter?.startDiscovery()
    }
}